<?php
namespace app\common\service\impl;
use app\common\enums\ResultEnum;
use app\common\exception\ZeduException;
use app\common\model\User;
use app\common\model\UserLog;
use app\common\service\UserService;
use think\facade\Log;
class UserServiceImpl implements UserService
{
    protected $user;
    protected $userLog;
    
    
    public function __construct(User $user,UserLog $userLog)
    {
        
        $this->user = $user;
        $this->userLog = $userLog;
       
    }

    public function save(array $data){
       $number = User::where(['number'=>$data['number']])->find();
        if($number){
            throw new ZeduException(['code'=>ResultEnum::WRITE_ERROR['code'],'msg'=>'身份证号/纳税人识别号已存在']);
        }
        $user = new User;
        try {
            $user->save($data);
            return true;
        } catch (\Exception $e) {
            Log::record('[ user插入数据 ]'.$e->getMessage(),'error');
            return false;
        } 
    }
    public function list(){
        $agency_id = session('admin.agency_id');
        if(strpos($agency_id, '1')===false){
           
            $user = User::where('agency_id','in',$agency_id)->select();
            
        }else{
            $user = User::all();
        }
        return $user;
        
    }
    public function index(array $where, int $page, int $length)
    {
        
        
        try {
            $model = User::with('agency')->where($where)->order('id', 'desc')
            ->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            
        } catch (\Exception $e) {
            Log::record('用户列表:'.$e->getMessage(),'error');
            return false;
        }
        return $model;
    }
    
    public function delete(int $id)
    {
        try {
            User::destroy($id);
        } catch (\Exception $e) {
            Log::record('[ 删除用户 ]:'.$e->getMessage(),'error');
            return false;
        }
       return true;
    }


    public function edit(array $data){
        $value = $this->user->where(['id'=>$data['id']])->value($data['key']);
        try {
            $res = $this->user->save([$data['key']=>$data['value']],['id'=>$data['id']]);
            if($res){
                $resLog = $this->recordLog($data,$value,$this->user[$data['key']]);
                if($resLog){
                    return $this->user;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (\Exception $e) {
            Log::record('[ 编辑用户列表 ]:'.$e->getMessage(),'error');
            return false;
        }

       
    }
    
    public function recordLog($data,$oldValue,$value){
        $type = [
            'name'=>1,
            'user_name'=>2,
            'number'=>3,
            'nick_name'=>4
        ];
        $log = ['user_id'=>$data['id'],'admin_id'=>session('admin.id'),'type'=>$type[$data['key']],'old_value'=>$oldValue,'value'=>$value];
        
        try {
            $this->userLog->create($log);
        } catch (\Exception $e) {
            Log::record('[ 写入数据 ]:'.$e->getMessage(),'error');
            return false;
        }
        
        return true;

    }

    public function userLog(array $data)
    {
        try {
            $model = $this->userLog::with('admin')->where(['user_id'=>$data['id']])->order('id', 'desc')
            ->paginate($data['length'],false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$data['page'],
            ]);
        } catch (\Exception $e) {
            Log::record('[ 查询日志 ]'.$e->getMessage(),'error');
            return false;
        }
        
        return $model;
    }

}

